<template>
	<!-- 添加弹框 -->
	<el-dialog title="" visible :show-close="false" :modal-append-to-body="false" class="wrap" @close="cancel" :close-on-click-modal="false">
		<i class="el-icon-close close" @click="cancel" style="color: white;z-index: 99;"></i>
		<div class="bt">添加新配件</div>
		<el-tabs type="card">
			<el-tab-pane label="新加配件">
				<el-form label-width="100px" class="form">
					<el-form-item label="分类名称">
						<el-select v-model="vwpflid" class="select-block" disabled>
							<el-option :label="v.mc" :value="v.id+''" v-for="v in comboxlist" ></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="配件名称">
						<el-input v-model="vmc" placeholder="请输入物品名称" class="select-block" style="width: 46%;display: inline-block;float: left;"></el-input>
						<span style="color: red;float: left;">建议格式：物品名称加‘—’加配件名称</span>
					</el-form-item>
					<el-form-item label="出售单价" v-if='pand'>
						<el-input v-model="vjg" v-if='pand' placeholder="请输入价格" class="select-block"></el-input>
					</el-form-item>
					<el-form-item label="单位名称">
						<el-select v-model="vdw" class="select-block" placeholder="请选择单位">
							<el-option :label="v.mc" :value="v.mc" v-for="v in dwcomboxlist"></el-option>
						</el-select>
					</el-form-item>
					<div class="search-item">
					</div>
					<!--<el-form-item label="是否上架">
						<el-tooltip placement="top" class="sx">
							<el-switch v-model="vsfsj" active-color="#13ce66" inactive-color="#ff4949" inactive-text="否" active-text="是" active-value="Y" inactive-value="N"></el-switch>
						</el-tooltip>
					</el-form-item>-->
					<!--<el-form-item label="是否配件" v-if='pj'>
				<el-tooltip placement="top" class="sx">
					<el-switch v-model="vispj" active-color="#13ce66" inactive-color="#ff4949" inactive-text="否" active-text="是" active-value="Y" inactive-value="N"></el-switch>
				</el-tooltip>
			</el-form-item> -->
					<el-form-item label="库存预警值">
						<el-input v-model="vkcxx" placeholder="请输入物品名称" class="select-block"></el-input>
					</el-form-item>
					<el-form-item label="物品描述">
						<el-input v-model="vms" type="textarea" :rows="2" placeholder="请输入描述" class="select-block"></el-input>
					</el-form-item>
					<el-button type="primary" class="submit" @click="submit">添加</el-button>
					<el-button type="danger" class="submit" @click="cancel">退出</el-button>
				</el-form>
			</el-tab-pane>
			<el-tab-pane label="选择现有配件" v-if="vif">
				<div style="width: 100%; text-align: right;">
					<el-tooltip placement="top" class="sx">
						<el-switch @change="search()" v-model="vsfsj" active-color="#ff4949" inactive-color="#13ce66" inactive-text="上架" active-text="下架" active-value="N" inactive-value="Y"></el-switch>
					</el-tooltip>
					<el-input placeholder="按物品名称搜索" style="width: 150px;" v-model="vkeyword" @keyup.enter.native="search" icon="el-icon-search"></el-input>
					<el-button type="primary" class="add btn" style='border-radius: 13px;' size="mini" :loading="searchLoading" @click="search" icon="el-icon-search"></el-button>
				</div>
				<el-table :data="dataList" border stripe align="left" size="mini" style="margin-top: 10px;" class="admin-table" @select="checkSelect" @select-all="checkall">
					<el-table-column type="selection" width="55"></el-table-column>
					<div slot='empty'>
						<img src="../../../assets/img/empty.png" style="width:40px;vertical-align: middle;">
						<span style="vertical-align: middle;">暂时还没有添加配件哦!</span>
					</div>
					<el-table-column :prop="v.prop" :label="v.label" v-for="(v,index) in portTitleList" align="center" show-overflow-tooltip v-if="v.hide != 0" :formatter="v.formatter" :align="v.align || 'center'" :fixed="v.fixed" :class-name="v.columnStyle" :width="v.width" :min-width="v.minWidth" :header-align="v.headerAlign || 'center'">
						<template scope="scope">
							<div slot="reference" :style="tableCellControl(scope)">
								{{ scope.row[scope.column.property] }}
							</div>
						</template>
					</el-table-column>
				</el-table>
					<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="pageindex" :page-sizes="pagesizes" :page-size="pagesize" :layout="layout" :total="total" class="class-page">
					</el-pagination>
				<div style="width: 100%; text-align: center;margin-top: 20px;">
					<el-button type="primary" class="submit" :disabled="multipleDeleteDisabled" @click="submit2">添加</el-button>
					<el-button type="danger" class="submit" @click="cancel">退出</el-button>
				</div>
			</el-tab-pane>
		</el-tabs>

	</el-dialog>
</template>
<script type="text/javascript">
	import qs from 'qs'
	export default {
		props: ['params'],
		data() {
			return {
				vkeyword: '',
				wpid: '',
				vif: true,
				ispj: '',
				sz: '',
				pand: true,
				vwpflid: '',
				vmc: '',
				vms: '',
				vjg: '',
				vdw: '',
				vsfsj: 'Y',
				vispj: 'Y',
				vwpurl: '',
				vkcxx: '100',
				dataList: [], //表格数据
				comboxlist: [],
				dwcomboxlist: [],
				multipleDeleteDisabled: true,
				flist: '',
				layout: 'total, sizes, prev, pager, next, jumper', //组件布局
				pageindex: this.$store.state.pageindex, //当前分页页码
				pagesizes: this.$store.state.pagesizes, //显示个数的选择器选项
				pagesize: this.$store.state.pagesize, //每页显示条目数
				total: 0, //总条数
				portTitleList: [

					{
						prop: 'mc',
						label: '配件名称',
						hide: 1,
						align: 'center',
						fixed: false,
						width: '',
						minWidth: '',
						headerAlign: 'center',
						formatter: function(row, column) {}
					},

					{
						prop: 'jg1',
						label: '单价/单位',
						hide: 1,
						align: 'center',
						fixed: false,
						width: '110',
						minWidth: '',
						headerAlign: 'center',
						formatter: function(row, column) {}
					},
					{
						prop: 'dw',
						label: '单位名称',
						hide: 0,
						align: 'center',
						fixed: false,
						width: '',
						minWidth: '',
						headerAlign: 'center',
						formatter: function(row, column) {}
					},
					{
						prop: 'sfsjmc',
						label: '是否上架',
						hide: 0,
						align: 'center',
						fixed: false,
						width: '',
						minWidth: '',
						headerAlign: 'center',
						formatter: function(row, column) {}
					},
					{
						prop: 'ispjmc',
						label: '配件',
						hide: 1,
						align: 'center',
						fixed: false,
						width: '80',
						minWidth: '',
						headerAlign: 'center',
						formatter: function(row, column) {}
					},
					{
						prop: 'kcsl',
						label: '库存数量',
						hide: 0,
						align: 'center',
						fixed: false,
						width: '',
						minWidth: '',
						headerAlign: 'center',
						formatter: function(row, column) {}
					},
					{
						prop: 'kcxx',
						label: '库存预警值',
						hide: 1,
						align: 'center',
						fixed: false,
						width: '110',
						minWidth: '',
						headerAlign: 'center',
						formatter: function(row, column) {}
					},

				],
			}
		},
		created() {
			//			console.log(this.data,'ffff')
			this.getcomboxlist();
			this.getdwcomboxlist();
			if(this.params) {
				if(this.params.row) {
					console.log(this.params, 'ssssssssssss')
					this.vwpflid = this.params.row.wpflid;
					this.wpfl = this.params.row.wpfl;
					this.vmc = this.params.row.mc;
					this.wpid = this.params.row.id;
					this.search()
				} else {
					this.vwpflid = this.params.vselectwpfxid + '';
					if(this.params.vif == false) {
						this.vif = false
					}
				}
			} else {
				this.vif = false
			}
		},
		methods: {
			search(sz) {
				//查询搜索触发事件
				var logininfo = JSON.parse(localStorage.logininfo);
				this.searchLoading = true;
				var params2 = qs.stringify({
					sfsj: this.vsfsj,
					keyword: this.vkeyword, // keyword
					wpflid: this.vwpflid, // 物品分类id
					wpfl: this.wpfl,
					ispj: this.vispj,
					yhid: logininfo.id,
					xxid: logininfo.xxid,
					pageindex: this.pageindex,
					pagesize: this.pagesize
				})
				//执行
				this.axios.post(this.$store.state.api + 'wpfx/list', params2)
					.then(res => {
						console.log(res, 'sss')
						let data = res.data.data;
						let list = data.list;
						this.total = data.totalPage;
						this.dataList = list;
						this.searchLoading = false;
					}).catch(res => {
						this.searchLoading = false;
					})
			},
			submit2() {
				//检测
				var arr = []
				this.multipleDelNum.forEach(function(v, i) {
					arr.push(v.id)
				})
				this.$store.state.loading = true;
				var logininfo = JSON.parse(localStorage.logininfo);
				var params3 = qs.stringify({
					wpid: this.wpid,
					pjids: arr + '',
					yhid: logininfo.id,
					xxid: logininfo.xxid
				});
				//执行
				this.axios.post(this.$store.state.api + 'wpfx/xzpj', params3)
					.then(res => {
						if(res.data.code == 1) {
							this.$emit('close', {
								type: 'pjadd',
								cancel: false
							});
							this.$message.success(res.data.msg);
							this.$store.state.loading = false;
						} else {
							this.$message.warning(res.data.msg);
							this.$store.state.loading = false;
						}
					}).catch(res => {
						this.$message.danger("服务器繁忙，请重试");
						this.$store.state.loading = false;
					})
			},
			submit() {
				//检测
				this.$store.state.loading = true;
				if(!this.vmc) {
					this.$message.warning('请选择物品分类名称');
					this.$store.state.loading = false;
					return;
				} else if(!this.vmc) {
					this.$message.warning('请输入物品名称');
					this.$store.state.loading = false;
					return;
				} else if(!this.vdw) {
					this.$message.warning('请选择单位');
					this.$store.state.loading = false;
					return;
				} else if(!this.vsfsj) {
					this.$message.warning('请选择是否上架');
					this.$store.state.loading = false;
					return;
				}
				var logininfo = JSON.parse(localStorage.logininfo);
				var params3 = qs.stringify({
					// wpflid: this.vwpflid,
					wpid: this.wpid,
					wpflid: this.vwpflid,
					mc: this.vmc,
					ms: this.vms,
					jg: this.vjg,
					dw: this.vdw,
					kcxx: this.vkcxx,
					sfsj: this.vsfsj,
					ispj: "Y",
					wpurl: this.vwpurl,
					yhid: logininfo.id,
					xxid: logininfo.xxid
				});

				//执行
				// console.log(params3)
				this.axios.post(this.$store.state.api + 'wpfx/insert', params3)
					.then(res => {
						if(res.data.code == 1) {
							this.$emit('close', {
								type: 'pjadd',
								cancel: false
							});
							this.$message.success(res.data.msg);
							this.$store.state.loading = false;
						} else {
							this.$message.warning(res.data.msg);
							this.$store.state.loading = false;
						}
					}).catch(res => {
						this.$message.danger("服务器繁忙，请重试");
						this.$store.state.loading = false;
					})
			},
			cancel() {
				this.$emit('close', {
					type: 'pjadd',
					cancel: true
				})
			},
			handleSizeChange(val) { //每页显示条数触发事件
				this.pagesize = val;
				this.pageindex = 1;
				this.search();
			},
			handleCurrentChange(val) { //分页触发事件
				this.search();
			},
			tableCellControl(scope) {
				//表格单元格控制CSS样式
				/*if (scope.row.id == 112 && scope.column.property == "iname") {
				    return {
				        color:"red",
				    }
				}else{
				    return {
				        color:"green",
				    }
				}*/
			},
			getcomboxlist() {
				var logininfo = JSON.parse(localStorage.logininfo);
				var params2 = qs.stringify({
					yhid: logininfo.id,
					xxid: logininfo.xxid,
					keywork: this.jcmc,
					pageindex: 1,
					pagesize: 1000
				})
				//执行
				this.axios.post(this.$store.state.api + 'wpfl/list', params2)
					.then(res => {
						let data = res.data.data;
						let list = data.list;
						this.comboxlist = list;
					}).catch(res => {
						this.searchLoading = false;
					})
			},
			getdwcomboxlist() {
				var logininfo = JSON.parse(localStorage.logininfo);
				var params2 = qs.stringify({
					yhid: logininfo.id,
					xxid: logininfo.xxid,
				})
				//执行
				this.axios.post(this.$store.state.api + 'bq/hddw', params2)
					.then(res => {
						let data = res.data.data;
						let list = data.list;
						this.dwcomboxlist = list;
					}).catch(res => {
						this.searchLoading = false;
					})
			},
			checkSelect(selection, row) { //点击单选select触发事件 
				this.multipleDelNum = selection
				if(this.multipleDelNum.length > 0) {
					this.multipleDeleteDisabled = false
				} else {
					this.multipleDeleteDisabled = true
				}
				console.log(selection)
			},
			checkall(selection) { //点击全选select触发事件 
				this.multipleDelNum = selection
				if(this.multipleDelNum.length > 0) {
					this.multipleDeleteDisabled = false
				} else {
					this.multipleDeleteDisabled = true
				}
				console.log(selection)
			},
		}
	}
</script>
<style lang="less" scoped>
	.wrap {
		.input-block {
			margin-bottom: 10px;
			text-align: center;
		}
		.bt {
			position: absolute;
			width: 100%;
			height: 50px;
			margin: 0 auto;
			top: 0;
			left: 0;
			background: #50BFFF;
			margin-bottom: 20px;
			color: white;
			text-align: left;
			line-height: 50px;
			font-size: 17px;
			padding-left: 15px;
		}
		.form {
			text-align: center;
			margin-top: 20px;
		}
		.input {
			display: inline-block;
		}
		.select-block {
			display: block;
			width: 85%;
		}
		.submit {
			width: 120px;
		}
		.flist {}
		.el-form-item {
			margin-bottom: 12px;
		}
	}
</style>